package io.dshubs.archives.controller;


import io.dshubs.archives.entity.ArchiveMyFavorite;
import io.dshubs.archives.service.ArchiveMyFavoriteService;
import io.dshubs.commons.log.annotation.LogOperation;
import io.dshubs.commons.tools.constant.Constant;
import io.dshubs.commons.tools.page.PageData;
import io.dshubs.commons.tools.utils.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.constraints.NotNull;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 我的收藏
 * </p>
 *
 * @author tan
 * @since 2021-08-27
 */
@Api(tags = "我的收藏")
@RestController
@RequestMapping("/myFavorite")
public class ArchiveMyFavoriteController {

    @Autowired
    private ArchiveMyFavoriteService archiveMyFavoriteService;


    @GetMapping("page")
    @ApiOperation("我的收藏:page")
    @LogOperation("我的收藏:/archive/collect:/img/icon-我的收藏.png:page")
    @ApiImplicitParams({
            @ApiImplicitParam(name = Constant.PAGE, value = "当前页码，从1开始", paramType = "query", required = true, dataType="int") ,
            @ApiImplicitParam(name = Constant.LIMIT, value = "每页显示记录数", paramType = "query",required = true, dataType="int") ,
            @ApiImplicitParam(name = Constant.ORDER_FIELD, value = "排序字段", paramType = "query", dataType="String") ,
            @ApiImplicitParam(name = Constant.ORDER, value = "排序方式，可选值(asc、desc)", paramType = "query", dataType="String"),
            @ApiImplicitParam(name = "fileNumber", value = "档号", paramType = "query", dataType="String")
    })

    public Result<PageData<ArchiveMyFavorite>> page(@ApiIgnore @RequestParam Map<String, Object> params){
        return new Result<PageData<ArchiveMyFavorite>>().ok(archiveMyFavoriteService.selectMyFavoritePage(params));
    }

    @PostMapping
    @ApiOperation("保存")
    @LogOperation("我的收藏:save")
    public Result save(@RequestBody ArchiveMyFavorite archiveMyFavorite){
        archiveMyFavoriteService.insertMyFavorite(archiveMyFavorite);
        return new Result();
    }

    @DeleteMapping
    @ApiOperation("删除")
    @LogOperation("我的收藏:delete")
    public Result delete(@RequestBody @NotNull(message = "收藏主键id不能为空")List<Long> ids){
        archiveMyFavoriteService.deleteMyFavorite(ids);
        return new Result();
    }

    @DeleteMapping("/cancle")
    @ApiOperation("取消收藏")
    @LogOperation("我的收藏:cancel")
    public Result cancel(@RequestBody @NotNull(message = "文档id不能为空") Long archiveId){
        archiveMyFavoriteService.cancleMyFavorite(archiveId);
        return new Result();
    }
}
